package com.ponhu.ea2.goodsCenter.provider.task;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ponhu.ea2.entity.LuxPrice;
import com.ponhu.ea2.entity.LuxSkuInfo;
import com.ponhu.ea2.goodsCenter.provider.mapper.LuxSkuInfoMapper;
import com.ponhu.ea2.goodsCenter.provider.schedule.LuxPriceTask;
import com.ponhu.ea2.goodsCenter.provider.service.LuxPriceService;
import com.ponhu.ea2.goodsCenter.provider.service.LuxSkuInfoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.util.CollectionUtils;
import sun.rmi.runtime.Log;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author luyu
 * @create 2021-09-27 20:48
 */
@Slf4j
public class LuxPriceByRunnableTask implements Runnable{

    @Resource
    LuxPriceService luxPriceService;

    LuxSkuInfoMapper luxSkuInfoMapper;

    private List luxPricelist;

    private String time;

    public LuxPriceByRunnableTask(LuxPriceService luxPriceService, List priceList,String param,LuxSkuInfoMapper luxMapper) {
        this.luxPricelist = priceList;
        this.luxPriceService = luxPriceService;
        this.luxSkuInfoMapper = luxMapper;
        this.time = param;
    }

    @Override
    public void run() {
        process(luxPricelist);
    }

    public void process(List list){
        List<LuxPrice> luxList = new ArrayList();
        // 遍历list,获取价格,执行插入操作
        for (int j = 0; j < list.size(); j++) {
            LuxPrice luxPrice = new LuxPrice();
            Map linkmap = (Map) list.get(j);
            if(!linkmap.isEmpty()){
                luxPrice.setSku(linkmap.get("sku").toString());
                luxPrice.setPrice(linkmap.get("price").toString());
                luxPrice.setCrawlDate(time);
                // 需要定义,如何获取汇率
                luxPrice.setRmbPrice(Double.parseDouble(linkmap.get("price_cny").toString()));
                luxList.add(luxPrice);

                // 检查sku是否存在
                LuxSkuInfo luxSkuInfo = luxSkuInfoMapper.selectOne(
                        new QueryWrapper<LuxSkuInfo>().eq("sku", linkmap.get("sku").toString()));
                if(ObjectUtils.isEmpty(luxSkuInfo)){
                    // 调用SKU详细字段查询
                    String sku = linkmap.get("sku").toString();
                    String url = "http://121.37.250.170:8085/goodsfind?sku="+sku;
                    Map map = LuxPriceTask.sendGet(url);
                    List pages = (ArrayList)map.get("pages");
                    luxSkuInfo = saveLuxSkuInfo(pages,time);// 保存sku详细信息
                    luxSkuInfoMapper.insert(luxSkuInfo);
                }
            }
        }
        if(!CollectionUtils.isEmpty(luxList)){
            log.info("保存的同行价条数为{}",luxList.size());
            luxPriceService.saveBatch(luxList);//批量保存同行价
        }
    }


    public LuxSkuInfo saveLuxSkuInfo(List pages,String time){
        if(!CollectionUtils.isEmpty(pages)){
            Map skuMap = (Map) pages.get(0);
            LuxSkuInfo luxSkuInfo = new LuxSkuInfo();
            luxSkuInfo.setSku(skuMap.get("sku")==null ? null : skuMap.get("sku").toString());
            luxSkuInfo.setBrand(skuMap.get("brand")==null ? null : skuMap.get("brand").toString());
            luxSkuInfo.setBrandId(skuMap.get("brand_id")==null ? null : skuMap.get("brand_id").toString());
            luxSkuInfo.setCategory(skuMap.get("category")==null ? null : skuMap.get("category").toString());
            luxSkuInfo.setColor(skuMap.get("color")==null ? null : skuMap.get("color").toString());
            luxSkuInfo.setColorId(skuMap.get("color_id")==null ? null : skuMap.get("color_id").toString());
            luxSkuInfo.setDetailUrl(skuMap.get("detail_url")==null ? null : skuMap.get("detail_url").toString());
            luxSkuInfo.setGrade(skuMap.get("grade")==null ? null : skuMap.get("grade").toString());
            luxSkuInfo.setGradeId(skuMap.get("grade_id")==null ? null : skuMap.get("grade_id").toString());
            luxSkuInfo.setMaterial(skuMap.get("material")==null ? null : skuMap.get("material").toString());
            luxSkuInfo.setMaterialId(skuMap.get("material_id")==null ? null : skuMap.get("material_id").toString());
            luxSkuInfo.setSeries(skuMap.get("series")==null ? null : skuMap.get("series").toString());
            luxSkuInfo.setSeriesId(skuMap.get("series_id")==null ? null : skuMap.get("series_id").toString());
            luxSkuInfo.setTitle(skuMap.get("title")==null ? null : skuMap.get("title").toString());
            luxSkuInfo.setVendor(skuMap.get("vendor")==null ? null : skuMap.get("vendor").toString());
            luxSkuInfo.setVendorId(skuMap.get("vendor_id")==null ? null : skuMap.get("vendor_id").toString());
            luxSkuInfo.setSize(skuMap.get("size2")==null ? null : skuMap.get("size2").toString());
            luxSkuInfo.setCreateTime(time);
            return luxSkuInfo;
        }
        return null;
    }
}
